package com.work.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.Calendar;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.chainsaw.Main;

import com.work.domain.UserAdmin;

/**
 * 
 * @author zhubingyi
 * 
 * 获取登陆日志类
 *
 */
public class GetLoginLog {
	
	//获取管理员登陆用户名字
	public static String getUserSession(HttpServletRequest request){
		UserAdmin ua  = (UserAdmin) request.getSession().getAttribute("adminuser");
		String userName=ua.getAdminname();
		return userName;
	}
	//获取管理员登陆用户角色
	public static String getUserRole(HttpServletRequest request){
		UserAdmin ua  = (UserAdmin) request.getSession().getAttribute("adminuser");
		String userRole=ua.getRole();
		return userRole;
	}
	//获取管理员登陆用户设备信息
	public static String getUserOsInfo(){
		String osName = System.getProperty("os.name");
		String osVersion = System.getProperty("os.version");
		String osArch = System.getProperty("os.arch");
		StringBuffer buf=new StringBuffer();
		StringBuffer append = buf.append("登陆用户操作系统："+osName);
		StringBuffer append1= append.append(" 操作系统架构："+osArch);
		StringBuffer append2 = append1.append(" 操作系统版本"+osVersion);
		return buf.toString();
	}
	//获取管理员登陆用户ip
	public static String getUserIp(HttpServletRequest request){
		String localAddr = request.getLocalAddr();
		return localAddr;
	}
	//获取管理员用户登陆时间
	public static String getDateTime() {
		Calendar date = Calendar.getInstance();
		java.text.SimpleDateFormat sim = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String str = sim.format(date.getTime());
		return str;

	}
    //获取登陆次数
	public static Integer getLoginCounts(Integer count){
        count++;	
		return count; 
	}
	//获取用户操作执行url
	public static  String getUserUrl(HttpServletRequest request){
		String url = request.getRequestURI();
		return url;
	}
	//获取用户操作 请求的端口和编码格式信息
	public static String getDoRequestLog(HttpServletRequest request){
		String localPort =String.valueOf( request.getLocalPort());
		String encoding = request.getCharacterEncoding();
		StringBuffer buf=new StringBuffer();
		StringBuffer append = buf.append("请求端口："+localPort);
		StringBuffer append1= append.append(" 请求编码："+encoding);
		return buf.toString();
	}
	
	/*public static void main(String[] args) {
		
		System.out.println(getUserOsInfo()+" "+getDateTime()+" "+getLoginCounts(1));
		
	}
	
	
	
	public static void getLog() throws IOException{
		
	HttpServletRequest request = null;  
		//System.out.println("浏览器基本信息："+request.getHeader("user-agent"));  
		System.out.println("客户端系统名称："+System.getProperty("os.name"));  
		System.out.println("客户端系统版本："+System.getProperty("os.version"));  
		System.out.println("客户端操作系统位数："+System.getProperty("os.arch"));  
		//System.out.println("HTTP协议版本："+request.getProtocol());  
		//System.out.println("请求编码格式："+request.getCharacterEncoding());  
		System.out.println("Accept："+request.getHeader("Accept"));  
		System.out.println("Accept-语言："+request.getHeader("Accept-Language"));  
		System.out.println("Accept-编码："+request.getHeader("Accept-Encoding"));  
		System.out.println("Connection："+request.getHeader("Connection"));  
		System.out.println("Cookie："+request.getHeader("Cookie"));  
		System.out.println("客户端发出请求时的完整URL"+request.getRequestURL());  
		System.out.println("请求行中的资源名部分"+request.getRequestURI());  
		System.out.println("请求行中的参数部分"+request.getRemoteAddr());  
		System.out.println("客户机所使用的网络端口号"+request.getRemotePort());  
		System.out.println("WEB服务器的IP地址"+request.getLocalAddr());  
		System.out.println("WEB服务器的主机名"+request.getLocalName());  
		System.out.println("客户机请求方式"+request.getMethod());  
		System.out.println("请求的文件的路径"+request.getServerName());  
		System.out.println("请求体的数据流"+request.getReader());  
		BufferedReader br=request.getReader();  
		String res = "";   
		while ((res = br.readLine()) != null) {    
			System.out.println("request body:" + res);     
		}  
		System.out.println("请求所使用的协议名称"+request.getProtocol());  
		System.out.println("请求中所有参数的名字"+request.getParameterNames());  
		Enumeration enumNames= request.getParameterNames();  
		while (enumNames.hasMoreElements()) {  
			String key = (String) enumNames.nextElement();  
			System.out.println("参数名称："+key);  
		}  
	}*/

}
